var express = require('express');
const myroute = express.Router()
const mySql = require('../model/index')

// 绩效管理渲染
myroute.get('/getAchievements',(req,res)=>{
  let sql=` SELECT * FROM performance LEFT JOIN admin ON performance.originator=admin.work_id 
  LEFT JOIN postadmin ON admin.jobs =postadmin.post_id LEFT JOIN department ON 
  postadmin.post_department=department.id  WHERE link='已完成'`
  mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,data:data})
    }
  }) 
})
// 绩效管理查询
myroute.post('/postSeach',(req,res)=>{
  // console.log(req.body.data);

  let sql=` SELECT * FROM performance LEFT JOIN admin ON performance.originator=admin.work_id LEFT JOIN 
  postadmin ON admin.jobs =postadmin.post_id LEFT JOIN department ON postadmin.post_department=department.id 
   WHERE link='已完成' AND performance.num LIKE '%${req.body.data.num}%' AND  admin.name LIKE '%${req.body.data.name}%' 
   AND department.department_name LIKE "%${req.body.data.department_name}%" AND  performance.audit_time>='${req.body.data.audit_time[0] || '1900-1-1'}' 
   and performance.audit_time<='${req.body.data.audit_time[1] || '2999-1-1'}' `
   mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,data:data})
    }
   })
})

// 绩效设置渲染
myroute.get('/getSetnews',(req,res)=>{
  let sql=`SELECT * FROM performance LEFT JOIN admin ON performance.originator=admin.work_id LEFT JOIN postadmin ON admin.jobs =postadmin.post_id 
  LEFT JOIN department ON postadmin.post_department=department.id`
  mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,data:data})
    }
  })
})
// 查询
myroute.post('/setpostSerch',(req,res)=>{
  console.log(req.body.data);
  let sql= `SELECT * FROM performance LEFT JOIN admin ON performance.originator=admin.work_id LEFT JOIN postadmin ON admin.jobs =postadmin.post_id 
  LEFT JOIN department ON postadmin.post_department=department.id where performance.nper LIKE "%${req.body.data.nper}%" and  postadmin.post_state 
  like '%${req.body.data.post_state}'` 
  mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,data:data})
    }
  })
})
//编辑修改
myroute.post('/setpostEdit',(req,res)=>{
  // console.log(req.body.data);
  let sql= `UPDATE postadmin set post_state='${req.body.data.state}' where post_id=${req.body.data.id}` 
  mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,msg:'修改成功'})

    }
  })
})
//删除
myroute.post('/setPostDel',(req,res)=>{
  // console.log(req.body.id)
  let sql=`DELETE FROM performance WHERE per_id=${req.body.id}`
  mySql.query(sql,(err,data)=>{
    if(err){
      res.send({code:1,err:err})
    }else{
      res.send({code:200,msg:'删除成功'})
    }
  })
})
// 添加
myroute.post('/setPostAdd',(req,res)=>{
  let num=Number( req.body.data.reviewer)
let sql=` INSERT INTO performance(per_id,num,nper,int_ime,reviewer,state) VALUES
(NULL,'${req.body.data.num}','${req.body.data.nper}','${req.body.data.int_ime}',${num},
'${req.body.data.state}')`
mySql.query(sql,(err,data)=>{
  if(err){
    res.send({code:1,err:err})
  }else{
    res.send({code:200,msg:'添加成功'})
  }
})
})

myroute.get('/get', (req, res) => {
  // console.log(req.query)
  const sql = `SELECT * FROM notice JOIN notice_class ON notice.notice_class=notice_class.notice_classId`
  mySql.query(sql, (err, data) => {
      if (err) {
          res.send({ code: 500, msg: '查询失败' })
      } else {
          res.send({ code: 200, data })
      }
  })
})

// 渲染公告详情
myroute.get('/show',(req,res)=>{
  console.log(req.query)
  const sql = `SELECT * FROM notice JOIN notice_class ON notice.notice_class=notice_class.notice_classId WHERE notice_id=${req.query.id}`
  mySql.query(sql, (err, data) => {
      if (err) {
          res.send({ code: 500, msg: '查询失败' })
      } else {
          res.send({ code: 200, data })
      }
  })
})


module.exports = myroute